package com.vn.toaa.karaoke.ui;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.TextView;

import com.vn.toaa.karaoke.database.DatabaseManager;
import com.vn.toaa.karaoke.database.SongModel;
import com.vn.toaa.karaoke.ui.Constant.SongCollection;
import com.vn.toaa.karaokecodelist.R;

public class DetailActivity extends Activity {
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.detail_activity);
		
		final TextView tvSongId = (TextView) findViewById(R.id.textview_song_id);
		final TextView tvAuthor = (TextView) findViewById(R.id.textview_author);
		final TextView tvLyric = (TextView) findViewById(R.id.textview_song_lyric);
		final TextView tvSongName = (TextView) findViewById(R.id.textview_song_name);
		final TextView tvSongCollection = (TextView) findViewById(R.id.textview_song_collection);
		final CheckBox cbFavorite = (CheckBox) findViewById(R.id.checkbox_favorite);
		final Button btBack = (Button) findViewById(R.id.button_back);
		final Button btFavorite = (Button) findViewById(R.id.button_favorite);
		
		final int songId = getIntent().getIntExtra(Constant.EXTRA_SONG_ID, 0);
		final int songCollection = getIntent().getIntExtra(Constant.EXTRA_SONG_COLLECTION, 0);
		SongModel songModel = null;
		if (songCollection == SongCollection.FIVE_NUMBER.ordinal()) {
			tvSongCollection.setText("Số 5");
			songModel = DatabaseManager.getInstance().selectSongById(songId, SongCollection.FIVE_NUMBER);
		} else {
			tvSongCollection.setText("Số 6");
			songModel = DatabaseManager.getInstance().selectSongById(songId, SongCollection.SIX_NUMBER);
		}
		if (songModel == null) {
			return;
		}
		
		tvSongId.setText(songModel.getSongId() + "");
		tvAuthor.setText(songModel.getAuthor().toUpperCase());
		tvLyric.setText(songModel.getLyric());
		tvSongName.setText(songModel.getTitle());
		tvSongName.setTextColor(Color.RED);
		tvAuthor.setTextColor(Color.BLUE);
		cbFavorite.setChecked(DatabaseManager.getInstance().isExistInFavorite(songModel.getSongId()));
		final SongModel tempSongModel = songModel;
		cbFavorite.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				if (isChecked) {
					DatabaseManager.getInstance().insertFavoriteSong(tempSongModel.getSongId());
				} else {
					DatabaseManager.getInstance().deleteFavoriteSong(tempSongModel.getSongId());
				}
			}
		});
		btBack.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				finish();
			}
		});
		btFavorite.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				startActivity(new Intent(DetailActivity.this, FavoriteActivity.class));
			}
		});
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
	}

}
